﻿using FCN;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2.Layer
{
   public class TanhLayer
    {
        float[,] convoutdata;
        public float[,] forward(float[,] x)
        {
            convoutdata = Matrix.activation_tanh(x).values;
            return convoutdata;
        }
        public float[,] backward(float[,] dout)
        {
            float[,] dx = Matrix.activation_tanhbackward(convoutdata, dout).values;

            return dx;
        }
        public Matrix[] convMatrix;
        public Matrix[] forward(Matrix[] x)
        {
            convMatrix = Matrix.activation_tanh(x);

            return convMatrix;
        }
        public float backward(float outdata, float dout)
        {
            float dx = dout * (1.0f - (float)(Math.Pow(outdata, 2)));

            return dx;
        }
        public float backward(int index,int x,int y,float dout)
        {
            float dx = backward(convMatrix[index].values[x,y], dout);

            return dx;
        }
        public Matrix[] backward(Matrix[] dout)
        {
            Matrix[] dx = Matrix.activation_tanhbackward(convMatrix, dout);

            return dx;
        }

        public Matrix[,] convoutdataMatrix;
        public Matrix[,] forward(Matrix[,] x)
        {
            convoutdataMatrix = Matrix.activation_tanh(x);

            return convoutdataMatrix;
        }
        public Matrix[,] backward(Matrix[,] dout)
        {
            Matrix[,] dx = Matrix.activation_tanhbackward(convoutdataMatrix, dout);

            return dx;
        }
        public float backward(int index,int indexy, int x, int y, float dout)
        {
            float dx = backward(convoutdataMatrix[index, indexy].values[x, y], dout);

            return dx;
        }
        float outdata;
        public float forward(float x)
        {
            outdata = (float)Math.Tanh(x);
            return outdata;
        }
        public float backward(float dout)
        {
            float dx = dout*(1.0f - (float)(Math.Pow(outdata, 2)));

            return dx;
        }
    }
}
